<html>
<head><meta charset="utf-8"><title>attribute macros on for loops? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html">attribute macros on for loops?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212221649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212221649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212221649">(Oct 04 2020 at 12:41)</a>:</h4>
<p>Can attribute macros affect <code>for</code> loops?<br>
In C++ the equivalent would be the use of OpenMP pragmas like:</p>
<div class="codehilite" data-code-language="C++"><pre><span></span><code><span class="cp">#pragma omp parallel for</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1024</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
</code></pre></div>



<a name="212221807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212221807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212221807">(Oct 04 2020 at 12:44)</a>:</h4>
<p>what does it do in C++ ?</p>



<a name="212222198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212222198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212222198">(Oct 04 2020 at 12:55)</a>:</h4>
<p>Attributes can annotate loops, see the <code>#[for_await]</code> in <a href="https://github.com/taiki-e/futures-async-stream#stream">https://github.com/taiki-e/futures-async-stream#stream</a> for an example</p>



<a name="212224765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212224765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212224765">(Oct 04 2020 at 14:06)</a>:</h4>
<p>Requires <code>#![feature(stmt_expr_attributes)]</code></p>



<a name="212226086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226086">(Oct 04 2020 at 14:43)</a>:</h4>
<p><span class="user-mention" data-user-id="209117">@lzutao</span>  forks and parallelizes chunks of the loop to a group of threads. joins all the threads when the loop finishes. i put it as an example</p>



<a name="212226131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226131">(Oct 04 2020 at 14:44)</a>:</h4>
<p>thanks! <span class="user-mention" data-user-id="300586">@veykril</span> <span class="user-mention" data-user-id="138448">@cuviper</span></p>



<a name="212226377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226377">(Oct 04 2020 at 14:51)</a>:</h4>
<p>thought you want to find the similar parallel feature in Rust, I was going to suggest rayon.</p>



<a name="212226708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226708">(Oct 04 2020 at 15:00)</a>:</h4>
<p>i know rayon but it is not as performant as OpenMP and there is a lot of HPC tooling around it too. i want to experiment if something could be done here.</p>



<a name="212226937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226937">(Oct 04 2020 at 15:06)</a>:</h4>
<p>OpenMP could be reimplemented through procs macros I think, but I don’t know if the performances would be here</p>



<a name="212226946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212226946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212226946">(Oct 04 2020 at 15:06)</a>:</h4>
<p>I think each month about a implementing the MPI spec in rust but it’s so big</p>



<a name="212227136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212227136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212227136">(Oct 04 2020 at 15:12)</a>:</h4>
<p>well, you have <a href="https://github.com/rsmpi/rsmpi">https://github.com/rsmpi/rsmpi</a> ;)</p>



<a name="212227139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212227139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio Sánchez <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212227139">(Oct 04 2020 at 15:12)</a>:</h4>
<p>why performance wouldn't be there?</p>



<a name="212228570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212228570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212228570">(Oct 04 2020 at 15:52)</a>:</h4>
<p>There are two crates for transforming a <code>for</code> loop to a rayon <code>for_each</code>:</p>
<ul>
<li>a <code>#[parallel]</code> attribute: <a href="https://crates.io/crates/rayon-attr">https://crates.io/crates/rayon-attr</a></li>
<li>a <code>parallel!(for ...)</code> macro: <a href="https://crates.io/crates/rayon-macro">https://crates.io/crates/rayon-macro</a></li>
</ul>



<a name="212228577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212228577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212228577">(Oct 04 2020 at 15:52)</a>:</h4>
<p>The latter works on stable Rust</p>



<a name="212228747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212228747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212228747">(Oct 04 2020 at 15:56)</a>:</h4>
<p>But in the end those are pretty limited tricks, no real advantage over writing the iterator in <code>for_each</code> style yourself</p>



<a name="212228869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212228869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212228869">(Oct 04 2020 at 15:59)</a>:</h4>
<p>I think you'd need deeper compiler integration to get closer to OpenMP,  more than the syntax-level access of proc-macros</p>



<a name="212232845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212232845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212232845">(Oct 04 2020 at 17:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F/near/212224765">said</a>:</p>
<blockquote>
<p>Requires <code>#![feature(stmt_expr_attributes)]</code></p>
</blockquote>
<p>Note that that isn't needed provided you use a preprocessor on, for instance, the whole function. See <a href="https://docs.rs/with_locals">https://docs.rs/with_locals</a> for an example</p>



<a name="212233465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/attribute%20macros%20on%20for%20loops%3F/near/212233465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/attribute.20macros.20on.20for.20loops.3F.html#212233465">(Oct 04 2020 at 17:54)</a>:</h4>
<p>Interesting, I hadn't thought of applying that at the function level.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>